package com.lej.share.ui

import android.content.Context
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.children
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BottomPopupView
import com.lej.share.R
import com.lej.share.databinding.ShareDialogDefaultBinding
import com.qmuiteam.qmui.kotlin.onClick
import com.umeng.socialize.UMShareListener
import com.umeng.socialize.bean.SHARE_MEDIA
import com.umeng.socialize.media.UMImage
import com.lej.share.core.ShareTarget

class ShareBottomSheetDialog(context: Context, val text: String, val image: UMImage? = null) : BottomPopupView(context) {
    override fun getImplLayoutId(): Int {
        return R.layout.share_dialog_default
    }

    lateinit var binding: ShareDialogDefaultBinding
    override fun onShow() {
        super.onShow()
        val activity = context as AppCompatActivity
        binding = ShareDialogDefaultBinding.bind(bottomPopupContainer.children.first())
        binding.tvCancel.onClick {
            dismiss()
        }
        val callback = object : UMShareListener {
            override fun onResult(p0: SHARE_MEDIA?) {
                p0.toString().toast()
            }

            override fun onCancel(p0: SHARE_MEDIA?) {
                "onCancel".toast()
            }

            override fun onError(p0: SHARE_MEDIA?, p1: Throwable?) {
                "onError".toast()
            }

            override fun onStart(p0: SHARE_MEDIA?) {
                "onStart".toast()
            }

        }
        binding.icWechat.onClick {
            ShareTarget.WEIXIN.share(activity, text = text, image = image)
        }
        binding.icWechatCircle.onClick {
            ShareTarget.WEIXIN_CIRCLE.share(activity, text = text, image = image)
        }
        binding.icQzone.onClick {
            ShareTarget.QZONE.share(activity, text = text, image = image)
        }
        binding.icWeibo.onClick {
            ShareTarget.SINA.share(activity, text = text, image = image)
        }
        binding.icLink.onClick {
            ShareTarget.COPY_LINK.share(activity, text = text)
        }
    }

    fun String.toast() {
        Toast.makeText(context, this, Toast.LENGTH_LONG).show()
    }

    companion object {
        fun show(context: Context,text: String,  image: UMImage? = null) {
            XPopup.Builder(context).asCustom(ShareBottomSheetDialog(context, text,image)).show()
        }
    }
}

